package com.angus.modules.front.service.impl;

import com.angus.api.front.domain.FrontUser;
import com.angus.api.front.model.FrontUserStatistics;
import com.angus.modules.front.mapper.FrontLoginInfoMapper;
import com.angus.modules.front.repository.FrontUserRepository;
import com.angus.modules.front.service.IFrontUserService;
import com.angus.modules.front.vo.StatisticsVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liyanan
 * @date 2024/10/15 1:38 PM
 * @description
 */
@Service
public class FrontUserServiceImpl implements IFrontUserService {

    @Resource
    private FrontUserRepository frontUserRepository;

    @Resource
    private FrontLoginInfoMapper frontLoginInfoMapper;

    @Override
    public FrontUser selectUserByUserName(String username) {
        return frontUserRepository.selectUserByUserName(username);
    }

    @Override
    public boolean checkUserNameUnique(FrontUser frontUser) {
        return frontUserRepository.checkUserNameUnique(frontUser);
    }

    @Override
    public boolean registerUser(FrontUser frontUser) {
        return frontUserRepository.registerUser(frontUser);
    }

    @Override
    public boolean updateUserProfile(FrontUser frontUser) {
        return frontUserRepository.updateUserProfile(frontUser);
    }

    @Override
    public FrontUser queryUserByPhoneNum(String device) {
        return frontUserRepository.selectUserByPhoneNum(device);
    }

    @Override
    public FrontUser queryUserByDevice(String device, Integer type){
        if(type==1){
            return frontUserRepository.selectUserByPhoneNum(device);
        }else{
            return frontUserRepository.selectUserByEmailNum(device);
        }
    }

    @Override
    public FrontUser queryUserById(Long userId) {
        return frontUserRepository.getById(userId);
    }

    @Override
    public List<FrontUser> queryAllUsers(String keyword, Integer status){
        return frontUserRepository.queryAllUsers(keyword, status);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteUserByIds(List<Long> ids){
        return frontUserRepository.deleteUserByIds(ids);
    }

    @Override
    public FrontUser queryUserByEmail(String email) {
        return frontUserRepository.queryUserByEmail(email);
    }

    @Override
    public StatisticsVo<Map<String, Object>> getUserStatistics() {

        //  注册量统计
        List<FrontUser> frontUsers = frontUserRepository.queryUserStatistics();
        FrontUserStatistics frontUserStatistics = new FrontUserStatistics();
        frontUserStatistics.setTotalUserCount((long) frontUsers.size());
        frontUserStatistics.setPaidUserCount(frontUsers.stream()
                .filter(frontUser -> 2 == frontUser.getUserType()).count());
        frontUserStatistics.setRegularUserCount(frontUsers.stream()
                .filter(frontUser -> 1 == frontUser.getUserType()).count());
        frontUserStatistics.setDisabledUserCount(frontUsers.stream()
                .filter(frontUser ->  "1".equals(frontUser.getStatus())).count());
        HashMap<String, Object> userCountMap = new HashMap<>();
        userCountMap.put("totalUserCount", frontUserStatistics.getTotalUserCount());
        userCountMap.put("paidUserCount", frontUserStatistics.getPaidUserCount());
        userCountMap.put("regularUserCount", frontUserStatistics.getRegularUserCount());
        userCountMap.put("disabledUserCount", frontUserStatistics.getDisabledUserCount());
        // 终端类型
        List<Map<String, Object>> terminalStats = frontLoginInfoMapper.getTerminalStatsGroupedByDate();
        // 浏览器类型
        List<Map<String, Object>> browserStats = frontLoginInfoMapper.getBrowserStatsGroupedByDate();
        // 归属地
        List<Map<String, Object>> ipLocationStats = frontLoginInfoMapper.getIpLocationStatsGroupedByDate();
        // 组合返回结构
        HashMap<String, Object> resData = new HashMap<>();
        resData.put("userCountMap", userCountMap);
        resData.put("terminalStats", terminalStats);
        resData.put("browserStats", browserStats);
        resData.put("ipLocationStats", ipLocationStats);
        // 抛出结果
        StatisticsVo<Map<String, Object>> vo = new StatisticsVo<>();
        vo.setData(resData);
        return vo;
    }
}
